package com.dingwei.superiordiary.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.CommunityNewsAdapter;
import com.dingwei.superiordiary.bean.CommunityNewsBean;
import com.dingwei.superiordiary.mvp.presenter.CommunityNewsPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.utils.DialogUtil;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/2/1.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class CommunityNewsActivity extends BaseActivity implements Contract.CommunityNewsView {

    CommunityNewsPresenter communityNewsPresenter = new CommunityNewsPresenter(this);
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;


    private CommunityNewsAdapter adapter;
    private int page = 1;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_community_news;
    }

    @Override
    protected void initData() {
        refreshLayout.autoRefresh();
    }

    @Override
    protected void initView() {
        tvTitle.setText("社区消息");
        adapter = new CommunityNewsAdapter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.parting_line), 1));
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        adapter.setCommunityNewInterfaceListener(new CommunityNewsAdapter.CommunityNewInterface() {
            @Override
            public void delete(final int position) {
                final CommunityNewsBean.DataBean.ListBean bean = adapter.getItem(position);
                DialogUtil.showAlertDialogChoose(BaseActivity.context, "温馨提示", "删除内容后其中的所有内容都会被删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case DialogUtil.BUTTON1:
                                dialog.dismiss();
                                break;
                            case DialogUtil.BUTTON2:
                                dialog.dismiss();
                                communityNewsPresenter.delete(bean.id, position);
                                break;
                        }
                    }
                });

            }

            @Override
            public void goFindDetail(int position) {
                final CommunityNewsBean.DataBean.ListBean bean = adapter.getItem(position);
                intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                intent.putExtra("id", bean.activity_id);
                startActivity(intent);
            }

            @Override
            public void goCommentDetail(int position) {
                final CommunityNewsBean.DataBean.ListBean bean = adapter.getItem(position);
                intent = new Intent(BaseActivity.context, MoreReplyActivity.class);
                intent.putExtra("id", bean.activity_id);
                intent.putExtra("pid", bean.comment.id);
                startActivity(intent);
            }
        });

        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                communityNewsPresenter.initData(page, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                communityNewsPresenter.initData(page, true, false);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.VISIBLE);
            if (adapter.getAllData() != null && adapter.getAllData().size() > 0) {
                mRecyclerView.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            } else {
                mRecyclerView.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        } else {
            refreshLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @OnClick(R.id.img_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    @Override
    public void onLoadMore(CommunityNewsBean bean) {
        adapter.addAll(bean.data.list);
        refreshLayout.finishLoadmore();
        showData("success");
    }


    @Override
    public void onRefresh(CommunityNewsBean bean) {

        adapter.clear();
        adapter.addAll(bean.data.list);
        refreshLayout.finishRefresh();
        showData("success");
    }

    @Override
    public void deleteSuccess(int position) {
        adapter.remove(position);
        showData("success");
        refreshLayout.autoRefresh();
    }

    @OnClick({R.id.img_back, R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.no_net_btn:
                page = 1;
                communityNewsPresenter.initData(page, true, false);
                break;
        }
    }
}
